/*
 * Project : Linuxbox
 * File    : UserManUpdate.java
 * Version : 1.0
 * Author  : Kevin Zhang
 *
 * Copyright (c) 2000-2001 by Linuxbox, Inc.
 * All rights reserved.
 */
package linuxbox.admin.servlet;

import javax.servlet.http.*;

import java.util.*;

import linuxbox.BoxStatic;
import linuxbox.admin.logic.*;
import linuxbox.admin.bean.*;
import linuxbox.util.*;
import linuxbox.common.*;

public class UserGrpUpdateAction extends Action {
	protected static String successJSP = null;
	
    public void doAction(HttpSession aSession, BoxSession aBoxSession, 
    HttpServletRequest req, HttpServletResponse res) throws Exception {
		BoxStatic.logger.debug("I am in UserGrpUpdate now ... ");
		
		int grpID = Integer.parseInt(req.getParameter("GROUP_ID"));
		String grpName = req.getParameter("GROUP_NAME");
		String desc = req.getParameter("DESC");
        String[] members = req.getParameterValues("listMember");
	
		BoxStatic.logger.debug("grpID is " + grpID);
		BoxStatic.logger.debug("grpName is " + grpName);
		BoxStatic.logger.debug("desc is " + desc);
		
		UserGrpObject ugo = new UserGrpObject(grpID, grpName, -1, desc);
		UserManager um = UserManager.getInstance();
		um.updateUserGrp(ugo);
		
        Collection c = um.getGrpMembers(grpID);
	    if (members != null) {
    		BoxStatic.logger.debug("members is not null");
            List addList = new ArrayList();
            List removeList = new ArrayList();
            processMemberList(c, members, addList, removeList);

    	    for (Iterator it = removeList.iterator(); it.hasNext(); ) {
    	        Integer id = (Integer)it.next();
    		    UserGrpMapObject ugmo = new UserGrpMapObject(id.intValue(), grpID);
        		um.removeUserGrpMap(ugmo);
            }
    	    for (Iterator it = addList.iterator(); it.hasNext(); ) {
    	        Integer id = (Integer)it.next();
    		    UserGrpMapObject ugmo = new UserGrpMapObject(id.intValue(), grpID);
        		um.addUserGrpMap(ugmo);
            }
        } else {
    		BoxStatic.logger.debug("members is null");
    	    for (Iterator it = c.iterator(); it.hasNext(); ) {
    	        UserCtrlKey key = (UserCtrlKey)it.next();
    		    UserGrpMapObject ugmo = new UserGrpMapObject(key.m_iID, grpID);
        		um.removeUserGrpMap(ugmo);
            }
        }
        
		redirectFile(successJSP, req, res);
		BoxStatic.logger.debug("I am leaving UserGrpUpdate now ... ");
	}

	private void processMemberList(Collection oldList, String[] newList,
	List addList, List removeList) {
	    for (int i = 0; i < newList.length; i++) {
    		BoxStatic.logger.debug("members is " + newList[i]);
	        addList.add(new Integer(newList[i]));
	    }
	    
	    for (Iterator it = oldList.iterator(); it.hasNext(); ) {
	        UserCtrlKey key = (UserCtrlKey)it.next();
	        removeList.add(new Integer(key.m_iID));
	    }
	    
	    CommonUtil.erraseCommon(addList, removeList);
    }
    	    
	public void setSuccessJSP(String str) {
        //"/linuxbox/jsp/admin/DeptAdmin.jsp"
		successJSP = str;
	}
}
